//
//  YAYISchedulingView.m
//  testForAreacode
//
//  Created by Veer on 16/6/3.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYISchedulingView.h"

#import "YAYILineView.h"

@interface YAYIItemView()

@property (nonatomic, weak) UIView *leftLineView;
@property (nonatomic, weak) UIView *topLineView;
@property (nonatomic, weak) UIView *rightLineView;
@property (nonatomic, weak) UIView *bottomLineView;

@property (nonatomic, assign) BOOL isOpen;


@end

@implementation YAYIItemView


- (void)setIsOpen:(BOOL)isOpen
{
    _isOpen = isOpen;
    
    self.backgroundColor = isOpen ? YAYIColor(59, 135, 242) : [UIColor whiteColor];
}

- (void)setLineType:(LineType)lineType
{
    if (LineTypeFirst == lineType) {
        self.leftLineView.hidden = NO;
    }else{
        self.leftLineView.hidden = YES;
    }
}

- (void)setBoderType:(BoderType)boderType
{
    if (boderType == BoderTypeRightBottom) {
        
        
    }else if (boderType == BoderTypeLeftTop){
    
        self.leftLineView.hidden = NO;
        self.topLineView.hidden = NO;
        
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = YAYIColor(104, 174, 240);
        
        UIView *leftLineView = [[UIView alloc] init];
//        leftLineView.hidden = YES;
        leftLineView.backgroundColor = YAYIColor(180, 180, 180);
        [self addSubview:leftLineView];
        self.leftLineView = leftLineView;
        
        UIView *topLineView = [[UIView alloc] init];
        topLineView.hidden = YES;
        topLineView.backgroundColor = YAYIColor(180, 180, 180);
        [self addSubview:topLineView];
        self.topLineView = topLineView;
        
        UIView *rightLineView = [[UIView alloc] init];
        rightLineView.backgroundColor = YAYIColor(180, 180, 180);
        [self addSubview:rightLineView];
        self.rightLineView = rightLineView;
        
        UIView *bottomLineView = [[UIView alloc] init];
        bottomLineView.backgroundColor = YAYIColor(180, 180, 180);
        [self addSubview:bottomLineView];
        self.bottomLineView = bottomLineView;
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.left.mas_equalTo(self);
        make.top.bottom.mas_equalTo(self);
    }];
    
    [self.rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.right.mas_equalTo(self);
        make.top.bottom.mas_equalTo(self);
    }];
    
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
    }];
}

@end



@interface YAYISchedulingView()

@property (nonatomic, weak) UIView *leftLineView;
@property (nonatomic, weak) UIView *topLineView;
@property (nonatomic, weak) UIView *rightLineView;
@property (nonatomic, weak) UIView *bottomLineView;
@property (nonatomic, weak) UILabel *titleLbl;
@property (nonatomic, weak) CAShapeLayer *border;
@property (nonatomic, strong) YAYIItemView *itemView;

@property (nonatomic, assign) NSInteger openCount;

@end


@implementation YAYISchedulingView


- (void)setIsOpen:(BOOL)isOpen
{
    self.openCount = isOpen ? 1 : 0;
}


- (void)setLineType:(LineType)lineType
{
    if (LineTypeFirst == lineType) {
        self.itemView.lineType = LineTypeFirst;
        self.leftLineView.hidden = NO;
        
    }else{
        self.itemView.lineType = LineTypeNormal;
        self.leftLineView.hidden = YES;
        
    }
}


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
        [self addGestureRecognizer:tap];

        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    // 边框
    YAYILineView *leftLineView = [[YAYILineView alloc] init];
    leftLineView.lineColor = YAYIColor(104, 174, 240);
    leftLineView.lineDirect = LineDirectV;
    [self addSubview:leftLineView];
    self.leftLineView = leftLineView;
    
    YAYILineView *topLineView = [[YAYILineView alloc] init];
    topLineView.lineColor = YAYIColor(104, 174, 240);
    [self addSubview:topLineView];
    self.topLineView = topLineView;
    
    YAYILineView *rightLineView = [[YAYILineView alloc] init];
    rightLineView.lineColor = YAYIColor(104, 174, 240);
    rightLineView.lineDirect = LineDirectV;
    [self addSubview:rightLineView];
    self.rightLineView = rightLineView;
    
    YAYILineView *bottomLineView = [[YAYILineView alloc] init];
    bottomLineView.lineColor = YAYIColor(104, 174, 240);
    [self addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;

    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"点击\n开启";
    titleLbl.font = [UIFont systemFontOfSize:15];
    titleLbl.textColor = YAYIColor(112, 108, 108);
    titleLbl.numberOfLines = 0;
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLbl];
    self.titleLbl = titleLbl;

    YAYIItemView *itemView = [[YAYIItemView alloc] init];
    itemView.backgroundColor = YAYIColor(92, 169, 239);
    itemView.hidden = YES;
    itemView.boderType = BoderTypeLeftTop;
    [self addSubview:itemView];
    self.itemView = itemView;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleItemTap:)];
    [itemView addGestureRecognizer:tap];
}


/**
 *  画虚线边框
 */
- (void)drawBorderWith:(CGRect)frame
{
    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = YAYIColor(104, 174, 240).CGColor;
    border.fillColor = nil;
//    CGRect frame1 = CGRectMake(0.5, 0.5, frame.size.width, frame.size.height);
    border.path = [UIBezierPath bezierPathWithRect:frame].CGPath;
    border.frame = frame;
    border.lineWidth = 1.0f;
    border.lineCap = @"square";
    border.lineDashPattern = @[@4, @4];
    [self.layer addSublayer:border];
    self.border = border;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 边框
    [self.leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(self);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(self);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(self);
        make.right.mas_equalTo(self).offset(-0.0);
        make.top.mas_equalTo(0);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(self);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self);
    }];
    

    [self.titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
        make.width.mas_equalTo(self);
    }];
    
//    CGFloat itemH = self.bounds.size.height / 4.0;
    
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self);
        make.width.mas_equalTo(self);
        make.height.mas_equalTo(self);
    }];
}


- (void)setOpenCount:(NSInteger)openCount
{
    _openCount = openCount;
    
    if (0 == self.openCount) {
        self.itemView.hidden = YES;
        self.border.hidden = NO;
    }else{
        self.itemView.hidden = NO;
        self.border.hidden = YES;
    }
}


/**
 *  设置单元格的状态
 */
- (void)setItemViewState:(BOOL)isOpen itemViewIndex:(NSInteger)index
{
    
    if (self.itemView.isOpen) {
        self.openCount++;
    }
}


#pragma mark - 点击事件
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
    if (self.isExpired) {
        [SVProgressHUD showImage:nil status:@"\n\n已过期排班\n\n"];
        return;
    }
    
    if (self.itemClickOperation(YES)) {
        self.openCount = 1;
    }else{
        self.openCount = 0;
        // 重复排班
        [SVProgressHUD showImage:nil status:@"\n\n重复排班\n\n"];
    }
}


- (void)handleItemTap:(UITapGestureRecognizer *)recognizer
{
    if (self.isExpired) {
        [SVProgressHUD showImage:nil status:@"\n\n已过期排班\n\n"];
        return;
    }

    self.openCount = 0;
    
    // 执行block
    self.itemClickOperation(NO);
}


@end
